import Artwork
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.ex3.R

// 定义图片数据类
data class Artwork(
    val imageRes: Int,
    val title: String,
    val author: String
)

class ImageGalleryActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MaterialTheme {
                Surface(modifier = Modifier.fillMaxSize()) {
                    ImageGalleryScreen()
                }
            }
        }
    }
}

@Composable
fun ImageGalleryScreen() {
    // 1. 状态管理：当前页面索引
    var currentPage by remember { mutableStateOf(0) }

    // 2. 图片数据列表（替换为你的图片资源）
    val artworks = listOf(
        Artwork(
            imageRes = R.drawable.img_rose,
            title = "Still Life of Blue Rose and Other Flowers",
            author = "Owen Scott (2021)"
        ),
        Artwork(
            imageRes = R.drawable.img_other1,
            title = "Sunset Landscape",
            author = "Emma Lee (2020)"
        ),
        Artwork(
            imageRes = R.drawable.img_other2,
            title = "City Night View",
            author = "Liam Wang (2022)"
        )
    )

    // 3. 垂直布局：图片区 + 按钮区
    Column(
        modifier = Modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        // 图片展示区（占大部分高度）
        Column(
            modifier = Modifier
                .weight(2f)
                .fillMaxWidth()
                .padding(16.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            // 当前图片
            Image(
                painter = painterResource(id = artworks[currentPage].imageRes),
                contentDescription = artworks[currentPage].title,
                modifier = Modifier
                    .fillMaxWidth()
                    .aspectRatio(1f) // 保持宽高比1:1
            )
            // 作品信息
            Spacer(modifier = Modifier.height(16.dp))
            Text(
                text = artworks[currentPage].title,
                fontSize = 18.sp,
                fontWeight = FontWeight.Bold
            )
            Text(
                text = artworks[currentPage].author,
                fontSize = 14.sp,
                color = MaterialTheme.colorScheme.onSurfaceVariant
            )
        }

        // 翻页按钮区（占小部分高度）
        Row(
            modifier = Modifier
                .weight(1f)
                .fillMaxWidth()
                .padding(16.dp),
            horizontalArrangement = Arrangement.SpaceEvenly
        ) {
            // 上一页按钮（第一页时禁用）
            Button(
                onClick = { currentPage-- },
                enabled = currentPage > 0,
                modifier = Modifier.weight(1f)
            ) {
                Text(text = "Previous")
            }

            Spacer(modifier = Modifier.width(16.dp))

            // 下一页按钮（最后一页时禁用）
            Button(
                onClick = { currentPage++ },
                enabled = currentPage < artworks.size - 1,
                modifier = Modifier.weight(1f)
            ) {
                Text(text = "Next")
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun GalleryPreview() {
    MaterialTheme {
        ImageGalleryScreen()
    }
}